package com.wangjiacai.utils.utils;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PingYinUtil {

    private static HanyuPinyinOutputFormat getFormat(){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        return format;
    }

    private static void getPinYin(String string, HanyuPinyinOutputFormat format, PinYinConsumer<String[]> consumer) throws BadHanyuPinyinOutputFormatCombination {
        char[] arr = string.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if (Character.toString(arr[i]).matches("[\\u4E00-\\u9FA5]+")) {
                consumer.consume(PinyinHelper.toHanyuPinyinStringArray(arr[i], format));
            } else {
                consumer.unKnow(arr[i]);
            }
        }
    }

    private interface PinYinConsumer<T>{
        void consume(T t);
        void unKnow(char ch);
    }

    private static String getPinYin(String inputString,HanyuPinyinOutputFormat format,String split){
        StringBuffer buffer = new StringBuffer();
        try {
            getPinYin(inputString, format, new PinYinConsumer<String[]>() {
                @Override
                public void consume(String[] strings) {
                    buffer.append(strings[0]);
                    buffer.append(split);
                }
                @Override
                public void unKnow(char ch) {
                    buffer.append(ch);
                    buffer.append(split);
                }
            });
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return null;
        }
        return buffer.toString();
    }

    /**
     * 将字符串中的中文转化为拼音,其他字符不变
     *
     * @param inputString
     * @return
     */
    public static String getPingYinWithOutTone(String inputString) {
        HanyuPinyinOutputFormat format=getFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        return getPinYin(inputString,format,"");
    }

    public static String getPingYinWithOutTone(String inputString,String split) {
        HanyuPinyinOutputFormat format=getFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        return getPinYin(inputString,format,split);
    }

    /**
     * 获取汉字串拼音，英文字符不变
     * @param inputString 汉字串
     * @return 汉语拼音
     */
    public static String getPingYinWithTone(String inputString) {
        HanyuPinyinOutputFormat format=getFormat();
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        return getPinYin(inputString,format,"");
    }

    public static String getPingYinWithTone(String inputString,String split) {
        HanyuPinyinOutputFormat format=getFormat();
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        return getPinYin(inputString,format,split);
    }
    /**
     * 获取汉字串拼音首字母，英文字符不变
     * @param inputString 汉字串
     * @return 汉语拼音首字母
     */
    public static String getFirstSpell(String inputString) {
        StringBuffer buffer = new StringBuffer();
        HanyuPinyinOutputFormat format=getFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            getPinYin(inputString, format, new PinYinConsumer<String[]>() {
                @Override
                public void consume(String[] strings) {
                    buffer.append(strings[0].charAt(0));
                }
                @Override
                public void unKnow(char ch) {
                    buffer.append(ch);
                }
            });
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return null;
        }
        return buffer.toString().replaceAll("\\W", "").trim();
    }

}
